package com.music.service.impl;

import com.laoxuai.beans.annotation.Autowired;
import com.laoxuai.beans.annotation.Service;
import com.laoxuai.ibatis.util.Example;
import com.laoxuai.ibatis.util.PageInfo;
import com.laoxuai.util.StringUtils;
import com.music.dao.BannerDao;
import com.music.pojo.Banner;
import com.music.service.BannerService;

import java.sql.SQLException;
import java.util.List;

@Service
public class BannerServiceImpl implements BannerService {

    @Autowired
    private BannerDao bannerDao;

    @Override
    public int insert(Banner banner) {
        try {
            //主键id不能重复,判断是否有id相同的图片
            Banner byPrimaryKey = bannerDao.getByPrimaryKey(banner.getId());
            if (byPrimaryKey!=null){
                //如果没有则执行插入
                return bannerDao.insert(banner);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delete(Integer id) {
        try {
            //查找是否有此id的图片
            Banner byPrimaryKey = bannerDao.getByPrimaryKey(id);
            if (byPrimaryKey!=null){
                return bannerDao.delete(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int update(Banner banner) {
        try {
            //查询是否含有此id的数据
            Banner byPrimaryKey = bannerDao.getByPrimaryKey(banner.getId());
            if (byPrimaryKey != null){
                return bannerDao.update(banner);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Banner getByPrimaryKey(Integer id) {
        try {
            return bannerDao.getByPrimaryKey(id);
        } catch (Exception e) {
        e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Banner> listPage(Banner banner, int page, int size) {
        try {
            return bannerDao.listPage(banner, page, size);
        } catch (SQLException throwables) {
        throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public PageInfo<Banner> listPageInfo(Banner banner, int page, int size) {
        try {
            return bannerDao.listPageInfo(banner, page, size);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Banner> selectByExample(Banner banner) {
        Example example = createExmaple(banner);
        try {
            return bannerDao.selectByExample(example);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    private Example createExmaple(Banner banner) {
        Example example = new Example(Banner.class);
        Example.Criteria criteria = example.createCriteria();
        // TODO
        if(banner != null){
            if(!StringUtils.isEmpty(banner.getId())){
                criteria.andEqualTo("id", banner.getId());
            }
            if(!StringUtils.isEmpty(banner.getImageUrl())){
                criteria.andEqualTo("imageUrl", banner.getImageUrl());
            }
            if(!StringUtils.isEmpty(banner.getTypeTitle())){
                criteria.andEqualTo("typeTitle", banner.getTypeTitle());
            }
            if(!StringUtils.isEmpty(banner.getType())){
                criteria.andEqualTo("type", banner.getType());
            }
            if(!StringUtils.isEmpty(banner.getBannerOrder())){
                criteria.andEqualTo("bannerOrder", banner.getBannerOrder());
            }
            if(!StringUtils.isEmpty(banner.getCreateTime())){
                criteria.andEqualTo("createTime", banner.getCreateTime());
            }
            if(!StringUtils.isEmpty(banner.getUpdateTime())){
                criteria.andEqualTo("updateTime", banner.getUpdateTime());
            }
            if(!StringUtils.isEmpty(banner.getIsDeleted())){
                criteria.andEqualTo("isDeleted", banner.getIsDeleted());
            }
        }
        return example;
    }

    /**
     * Banner 构建查询对象
     * @param banner
     * @return
     */
    public Example createExample(Banner banner){
        Example example = new Example(Banner.class);
        Example.Criteria criteria = example.createCriteria();
        if(banner != null){
            if(!StringUtils.isEmpty(banner.getId())){
                criteria.andEqualTo("id", banner.getId());
            }
            if(!StringUtils.isEmpty(banner.getImageUrl())){
                criteria.andEqualTo("imageUrl", banner.getImageUrl());
            }
            if(!StringUtils.isEmpty(banner.getTypeTitle())){
                criteria.andEqualTo("typeTitle", banner.getTypeTitle());
            }
            if(!StringUtils.isEmpty(banner.getType())){
                criteria.andEqualTo("type", banner.getType());
            }
            if(!StringUtils.isEmpty(banner.getBannerOrder())){
                criteria.andEqualTo("bannerOrder", banner.getBannerOrder());
            }
            if(!StringUtils.isEmpty(banner.getCreateTime())){
                criteria.andEqualTo("createTime", banner.getCreateTime());
            }
            if(!StringUtils.isEmpty(banner.getUpdateTime())){
                criteria.andEqualTo("updateTime", banner.getUpdateTime());
            }
            if(!StringUtils.isEmpty(banner.getIsDeleted())){
                criteria.andEqualTo("isDeleted", banner.getIsDeleted());
            }
        }
        return example;
    }
}
